package com.adams.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.adams.action.view.ViewAction;
import com.adams.common.Constants;
import com.adams.custom.page.core.service.DicTableService;
import com.ctc.wstx.util.StringUtil;

/**
 * 菜单ACTION
 * 
 * @author 904912
 * 
 */
public class DishesAction extends DcxtBaseAction {
    private static final long serialVersionUID = 6172575087796781323L;

    private DicTableService dicTableService;
    private File dishes_small_pic; // 小海报
    private String dishes_small_picFileName;
    private String dishes_small_picContentType;

    private File dishes_big_pic; // 大海报
    private String dishes_big_picFileName;
    private String dishes_big_picContentType;
    Logger logger = Logger.getLogger(DishesAction.class);
    private int[] bigPixle = new int[] { 480, 480 };
    private int[] smallposterPixle = new int[] {60, 60 };
    String dishesCode = "dcxt_dishes";
    String bussinessDir = "/dcxt";

    public String addDishesInfo() throws Exception {

        
            // 小海报
            Map inputMap = uploadPicAndCutImage(dishes_small_pic, dishes_small_picFileName,
                    dishes_small_picContentType, true, "dishes_small_pic", smallposterPixle, bussinessDir, "dishesmall");
            // 上传大海报
            Map inputMap2 = uploadPicAndCutImage(dishes_big_pic, dishes_big_picFileName, dishes_big_picContentType,
                    true, "dishes_big_pic", bigPixle, bussinessDir, "dishesbig", inputMap);

            Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(), menuId, formPanelId, inputMap2);
            if (map.get("success") != null && ((Boolean) map.get("success")) == true) {
                map.put("msg", "添加成功");
            }
            write(JSONObject.fromObject(map).toString());
       
        // 记录日志
        createLog("addDishesInfo", "添加菜单信息");
        return null;
    }

    public String updateDishesInfo() throws Exception {
        Map inputMap = ViewAction.requestParameterMap2Map(request);
        Map source = dicTableService.getDataByKey(dishesCode, inputMap);
        // 图片是否有更新标志
        boolean bigflag = false;
        boolean smalllag = false;
        String smallPic = (String) source.get("dishes_small_pic");
        if (smallPic != null && dishes_small_pic!=null) {
            inputMap = uploadPicAndCutImage(dishes_small_pic, dishes_small_picFileName, dishes_small_picContentType,
                    true, "dishes_small_pic", smallposterPixle, bussinessDir, "small");
            smalllag = true;
        }
        String bigPic = (String) source.get("dishes_big_pic");
        if (bigPic != null && dishes_big_pic!=null) {
            inputMap = uploadPicAndCutImage(dishes_big_pic, dishes_big_picFileName, dishes_big_picContentType, true,
                    "dishes_big_pic", bigPixle, bussinessDir, "big", inputMap);
            bigflag = true;
        }
        
        //删除图片
        if(bigflag){
            String filename = bigPic.substring(bigPic.lastIndexOf("/")+1);
            File bigFile = new File(Constants.adDir+bussinessDir+"/"+filename);
            bigFile.delete();
        }
        if(smalllag){
            String filename = smallPic.substring(smallPic.lastIndexOf("/")+1);
            File smallPicFile = new File(Constants.adDir+bussinessDir+"/"+filename);
            smallPicFile.delete();
        }
        //结束      
        Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(), menuId, formPanelId, inputMap);
        if (map.get("success") != null && ((Boolean) map.get("success")) == true) {
            map.put("msg", "修改成功");
        }
        write(JSONObject.fromObject(map).toString());
        return null;
    }

    public DicTableService getDicTableService() {
        return dicTableService;
    }

    public void setDicTableService(DicTableService dicTableService) {
        this.dicTableService = dicTableService;
    }

    public File getDishes_small_pic() {
        return dishes_small_pic;
    }

    public void setDishes_small_pic(File dishes_small_pic) {
        this.dishes_small_pic = dishes_small_pic;
    }

    public String getDishes_small_picFileName() {
        return dishes_small_picFileName;
    }

    public void setDishes_small_picFileName(String dishes_small_picFileName) {
        this.dishes_small_picFileName = dishes_small_picFileName;
    }

    public String getDishes_small_picContentType() {
        return dishes_small_picContentType;
    }

    public void setDishes_small_picContentType(String dishes_small_picContentType) {
        this.dishes_small_picContentType = dishes_small_picContentType;
    }

    public File getDishes_big_pic() {
        return dishes_big_pic;
    }

    public void setDishes_big_pic(File dishes_big_pic) {
        this.dishes_big_pic = dishes_big_pic;
    }

    public String getDishes_big_picFileName() {
        return dishes_big_picFileName;
    }

    public void setDishes_big_picFileName(String dishes_big_picFileName) {
        this.dishes_big_picFileName = dishes_big_picFileName;
    }

    public String getDishes_big_picContentType() {
        return dishes_big_picContentType;
    }

    public void setDishes_big_picContentType(String dishes_big_picContentType) {
        this.dishes_big_picContentType = dishes_big_picContentType;
    }

    

}
